﻿using System;
using System.Collections;

namespace JML.Core.DI
{
    /// <summary>
    /// A global, thread-safe hashtable.
    /// </summary>
    public class Globals
    {
        private static ILocalData current = new LocalData();


        public static ILocalData Data
        {
            get { return current; }
        }

        #region Nested type: LocalData

        private class LocalData : ILocalData
        {
            [ThreadStatic] private static Hashtable _hashtable;

            private bool _hasValues;

            private static Hashtable LocalHashtable
            {
                get { return _hashtable ?? (_hashtable = new Hashtable()); }
            }

            #region ILocalData Members

            public object this[object key]
            {
                get { return LocalHashtable[key]; }
                set
                {
                    _hasValues = true;
                    LocalHashtable[key] = value;
                }
            }

            public bool HasValues
            {
                get { return _hasValues; }
            }

            public void Clear()
            {
                _hasValues = false;

                LocalHashtable.Clear();
            }

            #endregion
        }

        #endregion
    }

    public interface ILocalData
    {
        object this[object key] { get; set; }
        bool HasValues { get; }
        void Clear();
    }
}